﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TrueSync;
using GameProtocols;
using Framework;

public class FightGameInitState : IGameState {

    FightGame mGame;

    public void Enter(IGameStateMachine game, params object[] param)
    {
        mGame = (FightGame)game;

        mGame.myPlayer = CreatePlayerEntity(405000012, 0);
        mGame.myPlayer.handle.GetLogicComponent<LogicMoveComponent>().Position = new TSVector2(-2, 0);

        mGame.otherPlayer = CreatePlayerEntity(405000012, 0);
        mGame.otherPlayer.handle.GetLogicComponent<LogicMoveComponent>().Position = new TSVector2(2, 0);

        ((FightCharacter)mGame.myPlayer.handle).mRival = mGame.otherPlayer;
        ((FightCharacter)mGame.otherPlayer.handle).mRival = mGame.myPlayer;

        ((FightCharacter)mGame.myPlayer.handle).GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Init);
        ((FightCharacter)mGame.otherPlayer.handle).GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Init);
    }

    public void Execute(PKG_FRAME frame)
    {
        if (mGame.myPlayer.IsValid == false)
            return;
        if (mGame.myPlayer.handle.GetLogicComponent<LogicStateMachineComponent>().CurrentState is StandState)
            return;

        mGame.ChangeState(new FightGamePlayState());
    }

    public void Exit()
    {
    }

    ObjHandle<LogicEntity> CreatePlayerEntity(int characterID, long playerID)
    {
        int entityID = GameManager.Instance.CreateFightCharacter(characterID, playerID);
        PlayerManager.Instance.AddNewPlayer(new Player() { PlayerID = playerID, ControllEntityID = entityID });
        return GameManager.Instance.GetEntity(entityID); 
    }
}
